package cn.itcast.erp.controller;

import cn.itcast.erp.annotation.CheckPower;
import cn.itcast.erp.entity.Emp;
import cn.itcast.erp.entity.Inventory;
import cn.itcast.erp.entity.Storedetail;
import cn.itcast.erp.entity.utils.AjaxResult;
import cn.itcast.erp.service.CStoreDetailService;
import cn.itcast.erp.service.InventoryService;
import org.apache.commons.io.IOUtils;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory")
public class InventoryController {

    @Resource
    private InventoryService inventoryService;


    @CheckPower("盘盈盘亏")
    @RequestMapping("/find")
    public AjaxResult findInventory(Inventory inventory) {
        return this.inventoryService.findInventory (inventory);
    }

    @CheckPower("盘盈盘亏")
    @RequestMapping("/findById")
    public Inventory findInventoryById(Long uuid) {
        return this.inventoryService.findInventoryById (uuid);
    }


    @CheckPower("盘盈盘亏")
    @RequestMapping("/insert")
    public AjaxResult insertInventory(@Validated Inventory inventory, @SessionAttribute("emp") Emp emp) {
        return this.inventoryService.insertInventory (emp.getUuid (),inventory);
    }

    @CheckPower("盘盈盘亏")
    @RequestMapping("/update")
    public AjaxResult updateInventory(@Validated Inventory inventory) {
        return this.inventoryService.updateInventory (inventory);
    }

    @CheckPower("盘盈盘亏")
    @RequestMapping("/check")
    public AjaxResult updateState(@SessionAttribute("emp") Emp emp, Long uuid) {
        return this.inventoryService.updateState (emp.getUuid (),uuid);
    }

    @CheckPower("盘盈盘亏")
    @RequestMapping("/delete")
    public AjaxResult deleteInventory(Long uuid){
        AjaxResult ar = new AjaxResult();
        int i = inventoryService.deleteInventory(uuid);
        if (i>0){
            ar.setMsg("删除成功");
        }else {
            ar.setMsg("删除失败");
        }
        return ar;
    }


    //数据校验异常处理
    @ExceptionHandler(BindException.class)
    public Map resolveBindingException(BindException ex){

        List<FieldError> errors = ex.getFieldErrors ( );
        List<String> list = new ArrayList<> ();
        if (errors.size ()!=0){
            for (FieldError error : errors) {
                String message = error.getDefaultMessage ( );
                list.add (message);
            }
        }
        return Collections.singletonMap ("message",list);
    }

    //时间转换器
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.addCustomFormatter (new DateFormatter ("yyyy-MM-dd"));
    }


    //导出Excel
    @CheckPower("盘盈盘亏")
    @RequestMapping("/export")
    public void getAllInventoryEx(HttpServletResponse response){
        try {
            response.reset();
            response.setContentType("application/msexcel;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("盘盈盘亏.xls", "UTF-8"));
            InputStream inputStream = inventoryService.getAllInventoryEx();
            IOUtils.copy(inputStream,response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
